package com.rpframework.module.common.bottom.service.impl;

import com.rpframework.core.json.ParameterException;
import com.rpframework.module.common.bottom.api.baidu.com.JsonMap;
import com.rpframework.module.common.bottom.api.baidu.com.Map;
import com.rpframework.module.common.bottom.entity.RegionCounty;
import com.rpframework.module.common.bottom.entity.po.Geography;
import com.rpframework.module.common.bottom.mapper.RegionCountyMapper;
import com.rpframework.module.common.bottom.service.RegionCountyService;
import com.rpframework.utils.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;


/**
 * 默认实现
 * Created by jump on 2016/7/4.
 */
@Service
public class RegionCountyServiceImpl extends BaseMapperImpl<RegionCounty> implements RegionCountyService {

    RegionCountyMapper mapper;

    @Autowired
    public RegionCountyServiceImpl(RegionCountyMapper mapper) {
        this.mapper = mapper;
        super.mapper = mapper;
    }

    @Override
    public Example getExample() {
        return new Example(RegionCounty.class);
    }

    @Override
    public RegionCounty selectByCountyCode(Integer countyCode) {
        return mapper.selectByCountyCode(countyCode);
    }

    @Override
    public String getAddress(Integer code, String... ds) {
        RegionCounty regionCounty = selectByCountyCode(code);
        if (regionCounty == null) {
            throw new ParameterException("CODE不正确");
        }
        String address = regionCounty.getRegionCity().getRegionProvince().getName() + regionCounty.getRegionCity().getName() + regionCounty.getName();
        if (!ArrayUtils.isEmpty(ds)) {
            for (String d : ds) {
                address += d;
            }
        }
        return address;
    }

    @Override
    public RegionCounty selectByCountyCodeValidate(Integer countyCode) {
        RegionCounty regionCounty = selectByCountyCode(countyCode);
        if (regionCounty == null) {
            throw new ParameterException("区县CODE不存在");
        }
        return regionCounty;
    }

    @Override
    public Geography getGridByDetailedAddress(String address) {
        JsonMap position = Map.getPosition(new Geography(address));
        Geography geography = new Geography();
        geography.setLatitude(position.getLocation().getLat());
        geography.setLongitude(position.getLocation().getLng());
        geography.setAddress(address);
        return geography;
    }

    @Override
    public Geography getGridByDetailedAddress(RegionCounty ec, String address) {
        String regionCountyName = ec.getName();
        if (StringUtils.isBlank(regionCountyName)) throw new ParameterException("区 NULL");

        if (ec.getRegionCity() == null || StringUtils.isBlank(ec.getRegionCity().getName()))
            throw new ParameterException("城市 NULL");

        if (ec.getRegionCity().getRegionProvince() == null || StringUtils.isBlank(ec.getRegionCity().getRegionProvince().getName()))
            throw new ParameterException("省市 NULL");

        return getGridByDetailedAddress(regionCountyName + ec.getRegionCity().getName() + ec.getRegionCity().getRegionProvince().getName() + address);
    }

    @Override
    public Geography getGridByDetailedAddress(Integer countyCode, String address) {
        return getGridByDetailedAddress(selectByCountyCodeValidate(countyCode), address);
    }
}
